<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class event_model extends CI_Model
{
    public function get_user_events($userID) {
        $this->events = array('upcoming' => array(), 'past' => array());

        $this->db->select('group_id');
        $query = $this->db->get_where('group_roster', array('user_id' => $userID, 'membership_status' => 1));

        if ($query->num_rows() < 1) return FALSE;

        $groupList = $query->result_array();

        foreach ($groupList as $group) {
            $this->get_upcoming($group['group_id']);
            $this->get_past($group['group_id']);
        }

        return $this->events;
    }

    public function get_group_events($groupID) {
        $this->events = array();
        $this->get_upcoming($groupID);
        $this->get_past($groupID);
        return $this->events;        
    }

    public function get_upcoming($groupID) {
        $this->db->select('
            event.event_id,
            event.event_name,
            event.location_name,
            event.start_date,
            event.start_time,
            event.end_time,
            group.group_id,
            group.group_name
        ');

        $this->db->order_by('event.start_date', 'DESC');
        $this->db->join('group', 'group.group_id = event.event_group');
        $query = $this->db->get_where('event', array('event.event_group' => $groupID, 'event.start_date >' => date("Y-m-d")));

        if ($query->num_rows() < 1) return FALSE;

        $eventList = $query->result_array();

        foreach ($eventList as $event) {
            $this->events['upcoming'][] = $event;
        }
    }

    public function get_past($groupID) {
        $this->db->select('
            event.event_id,
            event.event_name,
            event.location_name,
            event.start_date,
            event.start_time,
            event.end_time,
            group.group_id,
            group.group_name
        ');

        $this->db->order_by('event.start_date', 'DESC');
        $this->db->join('group', 'group.group_id = event.event_group');
        $query = $this->db->get_where('event', array('event.event_group' => $groupID, 'event.start_date <' => date("Y-m-d")));

        if ($query->num_rows() < 1) return FALSE;

        $eventList = $query->result_array();

        foreach ($eventList as $event) {
            $this->events['past'][] = $event;
        }
    }

    public function get_event_details($eventID) {
        $this->db->select('
            event.event_description,
            event.event_name,
            event.location_name,
            event.address1,
            event.address2,
            event.address3,
            event.city,
            event.state,
            event.zip,
            event.start_date,
            event.end_date,
            event.event_type,
            event.event_cost,
            event.start_time,
            event.end_time,
            group.group_id,
            group.group_name as event_group
        ');

        $this->db->join('group', 'group.group_id = event.event_group');
        $query = $this->db->get_where('event', array('event.event_id' => $eventID));

        if ($query->num_rows() != 1) return FALSE;
        return $query->row_array();
    }

    public function search_events($searchTerm) {
        $this->db->select('
            event.event_id,
            event.event_description,
            event.event_name,
            event.location_name,
            event.address1,
            event.address2,
            event.address3,
            event.city,
            event.state,
            event.zip,
            event.start_date,
            event.end_date,
            event.event_type,
            event.event_cost,
            event.start_time,
            event.end_time,
            group.group_id,
            group.group_name as event_group

        ');

        $this->db->order_by('event.event_name');
        $this->db->like('event.event_name', $searchTerm);
        $this->db->join('group', 'group.group_id = event.event_group');
        $query = $this->db->get('event');

        if ($query->num_rows() < 1) return FALSE;

       return $query->result_array();
    }

    public function insert_event($data = array()){
        //validate data
        if (count($data)<1){
            return false;
        }

        //insert_event
        $event_row = $this->db->insert('event',$data);

        return $event_row;
    }

    public function update_event($data = array()){
        //validate data
        if (count($data)<1){
            return false;
        }

        //update_event
        $this->db->where('id', $data['id']);
        $event_row = $this->db->update('event',$data);

        return $event_row;
    }
}